package com.fzshang.api.controller;


import com.fzshang.commen.bean.Attribute;
import com.fzshang.commen.bean.AttributeClassification;
import com.fzshang.commen.bean.layuiBean.ResultPage;
import com.fzshang.commen.bean.layuiBean.VoPage;
import com.fzshang.commen.controller.BaseController;
import com.fzshang.commen.entity.ApiResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 属性分类 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-02-01
 */
@Controller
@RequestMapping("/attribute")
public class AttributeController extends BaseController {

    /**
     * @return 主页面
     */
    @RequestMapping(value = "attribute.html")
    public String attribute(Model model){
        model.addAttribute("attributeClassification",attributeClassificationService.list());
        return "attribute/attribute";
    }

    /**
     * @return 添加页面
     */
    @RequestMapping(value = "attribute_add.html")
    public String attribute_add(Model model){
        model.addAttribute("attributeClassification",attributeClassificationService.list());
        return "attribute/attribute_add";
    }

    /**
     * @param model
     * @param id
     * @return 编辑页面
     */
    @RequestMapping(value = "attribute_edit.html")
    public String attribute_edit(Model model,Long id){
        model.addAttribute("attribute",attributeService.getById(id));
        model.addAttribute("attributeClassification",attributeClassificationService.list());
        return "attribute/attribute_edit";
    }

    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Attribute> selectMyPage(VoPage voPage, Attribute attribute){
        return attributeService.selectMyPage(voPage,attribute);
    }

    /**
     * @param attribute
     * @return 添加和修改
     */
    @RequestMapping(value = "saveOrUpdateById")
    @ResponseBody
    public ApiResult saveOrUpdateById(@RequestBody Attribute attribute){
        attribute.setCreateTime(System.currentTimeMillis());
        if(attributeService.saveOrUpdate(attribute)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param id
     * @return 删除
     */
    @RequestMapping(value = "deleteById")
    @ResponseBody
    public ApiResult deleteById(Long id){
        if(attributeService.removeById(id)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param id
     * @return 根据id查询信息
     */
    @RequestMapping(value = "selectById")
    @ResponseBody
    public ApiResult selectById(Long id){
        return ApiResult.ok(attributeService.getById(id));
    }

}
